package com.dianrui.agent.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.dianrui.agent.R;
import com.dianrui.agent.bean.Maintenance;

import org.json.JSONArray;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MaintenanceOrderListAdapter extends BaseAdapter {
    Context context;
    BatteryClickListener batteryClickListener;
    List<Maintenance> maintenanceList;


    public MaintenanceOrderListAdapter(Context context, List<Maintenance> repairList) {
        this.context = context;
        this.maintenanceList=repairList;
    }
    public void refresh(List<Maintenance> list) {
        this.maintenanceList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (maintenanceList == null) {
            return 0;
        }
        return maintenanceList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface BatteryClickListener {
        void onBatteryClick(String order_code, String number, String title, String contents, String create_time, String update_time, JSONArray photo);

    }

    public void setOnBatteryClickListener(BatteryClickListener messageClickListener) {
        this.batteryClickListener = messageClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_maintence_order_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (maintenanceList.size()>0) {
            final Maintenance Maintenance = maintenanceList.get(position);
            holder.carNumbers.setText(Maintenance.order_code);
            holder.number.setText(Maintenance.number);
            holder.pjoectname.setText(Maintenance.form_name);
            holder.create_time.setText(Maintenance.create_time);
            if (Maintenance.state.equals("0")) {
                holder.battery_status.setText("未处理");
                holder.battery_status.setTextColor(Color.RED);
            }
            if (Maintenance.state.equals("1")) {
                holder.battery_status.setText("已结束");
                holder.battery_status.setTextColor(Color.RED);
            }


            holder.click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    batteryClickListener.onBatteryClick(Maintenance.order_code,Maintenance.number,Maintenance.title,Maintenance.contents,Maintenance.create_time,Maintenance.update_time,Maintenance.photo);
                }
            });

        }

        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.numbers)
        TextView carNumbers;
        @BindView(R.id.pjoectname)
        TextView pjoectname;
         @BindView(R.id.battery_status)
         TextView battery_status;
         @BindView(R.id.number)
         TextView number;
         @BindView(R.id.create_time)
         TextView create_time;
         @BindView(R.id.repair_item_layout)
        LinearLayout click;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
